<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="主键" prop="id">
        <el-input v-model="dataForm.id" placeholder="主键"></el-input>
      </el-form-item>
      <el-form-item label="会员id" prop="accountId">
        <el-input v-model="dataForm.accountId" placeholder="会员id"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="name">
        <el-input v-model="dataForm.name" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="是否默认 1是0否" prop="isDefault">
        <el-input v-model="dataForm.isDefault" placeholder="是否默认 1是0否"></el-input>
      </el-form-item>
      <el-form-item label="省编号" prop="provinceCode">
        <el-input v-model="dataForm.provinceCode" placeholder="省编号"></el-input>
      </el-form-item>
      <el-form-item label="市编号" prop="cityCode">
        <el-input v-model="dataForm.cityCode" placeholder="市编号"></el-input>
      </el-form-item>
      <el-form-item label="区编号" prop="districtCode">
        <el-input v-model="dataForm.districtCode" placeholder="区编号"></el-input>
      </el-form-item>
      <el-form-item label="省市区名称" prop="area">
        <el-input v-model="dataForm.area" placeholder="省市区名称"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        accountId: "",
        name: "",
        mobile: "",
        isDefault: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        area: "",
        address: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/noc/taddress/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/noc/taddress", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
